/*
    Copyright (c);2019-2022;Wiscom System;

	All rights reserved.

    Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. Neither the name of the Wiscom System nor the names of its contributors
       may be used to endorse or promote products derived from this software without
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/

#include "los_hwi.h"
#include "los_task.h"
#include "los_api_bsp_watchdog.h"
#include "los_inspect_entry.h"
#include "csg_sdk_hal.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cpluscplus */
#endif /* __cpluscplus */

static UINT32 g_uwTskHiID;
UINT8 g_WATCHDOG_flag=1;
#define TSK_PRIOR_HI 4

static UINT32 Example_TaskHi(VOID)
{
    UINT32 uwRet = LOS_OK;
    UINT32 TempData = 0;
    dprintf("\r\n1. send '0xA4' to init WATCHDOG, send '0x54' to skip WATCHDOG!\r\n");
    while (g_WATCHDOG_flag==1)
    {
        LOS_TaskDelay(1000);
        TempData=(USART0->RXDATA);
        if(TempData& USART_RXDATA_EMPTY)
        {
            TempData=0;
        }
        else
        {
            TempData=(TempData & 0xFF);
        }
        // TempData=((USART0->RXDATA)& 0xFF);
        if(TempData==0xA4)
        {
            wwdg_config(WWDG0,127, 127, WWDGT_CFG_PSC_DIV8);
            dprintf("\r\n2. WATCHDOG init success!\r\n");
            g_WATCHDOG_flag=0;
        }
        else if(TempData==0x54)
        {
            dprintf("\r\n2. skip the watchdog!\r\n");
            g_WATCHDOG_flag=0;
        }
    }
    if(TempData==0x54)
    {
        g_WATCHDOG_flag=1;
    }
    while (g_WATCHDOG_flag==0)
    {
        wwdg_counter_update(WWDG0,127);
        dprintf("\r\n3. wwdg_counter_update success!\r\n");
        TempData=(USART0->RXDATA);
        if(TempData& USART_RXDATA_EMPTY)
        {
            TempData=0;
        }
        else
        {
            TempData=(TempData & 0xFF);
        }
        // TempData=((USART0->RXDATA)& 0xFF);
        if(TempData==0xA4)
        {
            dprintf("\r\n4. stop wwdg_counter_update, wait for reset!\r\n");
            while(1);
        }
        LOS_TaskDelay(10);
    }
    g_WATCHDOG_flag=1;
    LOS_InspectStatusSetByID(LOS_INSPECT_BSP_WATCHDOG,LOS_INSPECT_STU_SUCCESS);
    LOS_TaskDelete(g_uwTskHiID);
    return LOS_OK;
}

UINT32 Example_BSP_WATCHDOG(VOID)
{
    UINT32 uwRet;
    TSK_INIT_PARAM_S stInitParam;
    dprintf("\r\n--------4.27.4 BSP WATCHDOG test start!--------\r\n");
    /* lock task shcedue */
    LOS_TaskLock();

    stInitParam.pfnTaskEntry = (TSK_ENTRY_FUNC)Example_TaskHi;
    stInitParam.usTaskPrio = TSK_PRIOR_HI;
    stInitParam.pcName = "HIGH_NAME";
    stInitParam.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
    /* create high prio task */
    uwRet = LOS_TaskCreate(&g_uwTskHiID, &stInitParam);
    if (uwRet != LOS_OK)
    {
        LOS_TaskUnlock();

        dprintf("\r\nExample_TaskHi create Failed!\r\n");
        return LOS_NOK;
    }
    /* unlock task schedue */
    LOS_TaskUnlock();

    return uwRet;
}

#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cpluscplus */
#endif /* __cpluscplus */
